<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-10-27 11:32:59
 * @version $Id$
 */

namespace app\admin\controller;

use think\Request;
use think\Db;
use Godok\Org\Auth;
use think\Config;

class Rebate extends Base {
    
    function index(){

    	$iw=Request()->param();

    	$start_time=isset($iw['start_time'])?strtotime($iw['start_time']):'';
    	$end_time=isset($iw['end_time'])?strtotime($iw['end_time']):'';

    	if($end_time){
            $_end_time=getOneDay($end_time);
            // $_end_time=$_end_time['et'];
            if ($end_time==$_end_time['st']) {
                $end_time=$_end_time['et'];
            }
        }else{
            $end_time=getOneDay();
            $end_time=$end_time['et'];
        }

        if ($start_time) {
            // $start_time=getOneDay($start_time);
            // $start_time=$start_time['st'];
        }else{
            $start_time=getDayRegion($end_time,30);
            $start_time=$start_time['st'];
        }
        
        $iw['start_time']=date('Y-m-d H:i:s',$start_time);
        $iw['end_time']=date('Y-m-d H:i:s',$end_time);
        if ($iw['selt']!=='0') {
            $iw['selt']=$iw['selt']?:9;
        }
    	$data['w']=$iw;

        $list = Db::name('rebate')->order('breaktime asc')->select();
		$pre=date('H:i',strtotime('00:00'));$arr=[];
		foreach ($list as $k => $v) {
			if ($v['breaktime'] != 0) {
				$v['stime'] = $pre;
				$v['etime'] = date('H:i',strtotime($v['breaktime']));
				$pre = $v['etime'];
				$arr[$k] = $v;
			}
		}

		$today = date('Y-m-d');
		$gjor = Db::field('o.order_money as money,e.id,o.pay_time,e.name')
			->table('sdzx_employee')->alias('e')
			->join('sdzx_order o',"e.id=o.agent_id and  o.pay_status=1 and o.pay_time  between '{$iw['start_time']}' and '{$iw['end_time']}'",'left')
			->join('sdzx_employee_group_relation egr','e.id=egr.uid')
			->join('sdzx_employee_group g','egr.group_id=g.id')
			->where("g.code='GJ'")
			->order('e.id asc')
			// ->where("o.pay_time like '$today%' and o.pay_status=1")
			->select();
		
		$tmp = [];
		foreach ($gjor as $k => $v) {
			foreach ($arr as $kk => $vv) {
				$pt = date('Hi',strtotime($v['pay_time']));
				if ($pt >= date('Hi',strtotime($vv['stime'])) && $pt <= date('Hi',strtotime($vv['etime'])) ) {
					$tmp[$v['id']]['employee_name'] = $v['name'];


					$tmp[$v['id']]['time'][$vv['stime'].'-'.$vv['etime']]['money'] += $v['money'];
					$tmp[$v['id']]['time'][$vv['stime'].'-'.$vv['etime']]['precent'] = $vv['precent'];
					
				}else{
					$tmp[$v['id']]['employee_name'] = $v['name'];


					$tmp[$v['id']]['time'][$vv['stime'].'-'.$vv['etime']]['money'] += 0;
					$tmp[$v['id']]['time'][$vv['stime'].'-'.$vv['etime']]['precent'] = $vv['precent'];
					

				}
			}
		}
		$data['times']=$arr;
		$data['list']=$tmp;
		return $this->fetch('',$data);
    }

    public function setTime()
    {
    	$param = request()->param();
    	if (request()->isPost()) {
    		$arr = $param['arr'];
    		foreach ($arr as $k => $v) {
    			if ($v['breaktime']=='') {
    				Db::name('rebate')->where(['rebate_id'=>$v['id']])->update([
    					'breaktime' => '',
    					'precent' => ''
    				]);
    				unset($arr[$k]);
    			}
    		}

    		if (sizeof(array_unique(array_column($arr, 'breaktime'))) != sizeof($arr) ) {
    			$this->error('时间点不能重复');
    		}
    		foreach ($arr as $k => $v) {
    				Db::name('rebate')->where(['rebate_id'=>$v['id']])->update([
    					'breaktime' => ($v['breaktime'] == '')?'':date('Y-m-d H:i:s',strtotime($v['breaktime'])),
    					'precent' => $v['precent']
    				]);
    		}
    		$this->success('保存成功','back');
    	}else{

    		$list = Db::name('rebate')->order('rebate_id asc')->select();

    		foreach ($list as $k => $v) {
    			$list[$k]['breaktime'] = ($v['breaktime'] == 0)?'':date('H:i',strtotime($v['breaktime']));
    		}
    		
    		return $this->fetch('',['list'=>$list]);
    	}
    }
}